<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-row-waiting-widget" :style="getStyle('head')">
      <div v-for="(item,key) in queuePatientInfos" class="queue" :key="`${item.workerId}-${key}`">
        <div class="queue-inside">
          <div class="dept" :style="getStyle('queueName')">
            <ScrollX>{{ item[optionsInfo.registerNameKey] || (item.registerObjName) || '\u00a0' }}</ScrollX>
          </div>
          <div class="wait-patient" :key="widgetKey">
            <vue-seamless-scroll
                class="scroll"
                :hoverStop="false"
                :data="getPatientList(item)"
                @ScrollEnd="handleScrollEnd"
                :class-option="classOptionPatient(getPatientList(item) || [])"
                :ref="`scroll-${item.workerId}`"
            >
              <div class="inside-content">
                <template v-if="!!(getPatientFillList(item)?.length)">
                  <template v-for="(patientItem,patientKey) in getPatientFillList(item)">
                    <div class="wait-patient-inside" :style="getStyle('waitPatient')"
                         :key="`${widgetKey}-${item.workerId}-${patientKey}`"
                         v-html="getWaitPatientContent(patientItem)">
                    </div>
                  </template>
                </template>
                <template v-else>
                  <div class="wait-patient-inside-empty"/>
                </template>
              </div>
            </vue-seamless-scroll>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import emitter from "@/utils/designer/emitter";
import {pxToVw} from "@/utils/common";
import {v4 as uuid} from 'uuid';
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import vueSeamlessScroll from 'vue-seamless-scroll';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {buildParentApi} from "@/utils/designer/util";

let buildParent = buildParentApi();
export default {
  name: "scene-waiting-row2-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  components: {
    ScrollX,
    CompOperate,
    vueSeamlessScroll
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object(),
    }
  },


  computed: {
    // 温馨提示的滚动
    classOptionPatient() {
      return function (row) {
        let {openScroll, waitingNumber, waitingVisitStep, viewNumber} = this.optionsInfo;
        return {
          step: [1, 1.5, 2, 2.5, 3][waitingVisitStep - 1], // 数值越大速度滚动越快
          hoverStop: true,
          direction: 1, // 0向下 1向上 2向左 3向右
          limitMoveNum: (openScroll && (row?.length >= viewNumber)) ? waitingNumber : 99999, // 开始无缝滚动的数据量 this.dataList.length
        };
      }
    },

    // 就诊和候诊的className名
    getClassName() {
      return function (val) {
        switch (val) {
          case "treatingPatient":
            return "current-patient";
          case "waitPatientList":
            return "wait-patient";
          case "waitPatientCount":
            return "patient-count";
          case "registerObjNames":
            return "register-name";
        }
      };
    },

    // 设置的各元素的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "height", pxToVw(info.contentHeight));
            return Obj;
          case "queueName":
            Reflect.set(Obj, "color", (info.headerFontColor));
            Reflect.set(Obj, "backgroundColor", (info.headerBackgroundColor));
            return Obj;
          case "waitPatient":
            if (info?.patientDivider) {
              Reflect.set(Obj, "borderColor", 'rgba(51, 51, 51, 0.2)');
            } else {
              Reflect.set(Obj, "borderColor", 'transparent');
            }
            Reflect.set(Obj, "lineHeight", pxToVw((info.contentHeight - 90) / info.viewNumber));
            Reflect.set(Obj, "height", pxToVw((info.contentHeight - 90) / info.viewNumber));
            Reflect.set(Obj, "fontSize", pxToVw(info.patientFontSize));
            Reflect.set(Obj, "fontWeight", (info.patientFontWeight));
            return Obj;
        }
      };
    },

    // 列表的队列的患者queue
    queuePatientInfos() {
      let devType = (this.devType);
      let {showRowNums} = (this.optionsInfo);
      let queuePatientList = this.$store.getters['getWaitingPatientInfo'];
      let listInfo = devType ? (queuePatientList.apiData) : (queuePatientList.mockData);
      if (listInfo?.length > showRowNums) {
        return listInfo.slice(0, showRowNums);
      } else {
        let arr = (new Array(showRowNums - listInfo?.length).fill({}));
        return [...listInfo, ...arr];
      }
    },

    // 列表候诊患者
    getWaitPatient() {
      return function (row) {
        let {viewNumber = 8} = this.optionsInfo
        if (row?.waitPatientList?.length) {
          let len = row?.waitPatientList?.length;
          console.log(row)
          if (len >= viewNumber) {
            return row.waitPatientList
          } else {
            let fillArr = Array(viewNumber - len).fill({});
            return [...row.waitPatientList, ...fillArr]
          }
        } else {
          return Array(viewNumber).fill({});
        }
      }
    },

    // 合并当前队列的候诊就诊患者
    getPatientList() {
      return function (row) {
        let {showCurrentPatient = 2} = this.optionsInfo;
        let currentPatient = row?.treatingPatient || []
        let waitPatient = row?.waitPatientList || [];
        if (showCurrentPatient === 2) {
          return [...currentPatient, ...waitPatient]
        } else {
          return [...waitPatient]
        }
      }
    },

    // 合并当前队列的候诊就诊患者并填充数据
    getPatientFillList() {
      return function (row) {
        let {viewNumber = 8} = this.optionsInfo;
        let mergePatient = this.getPatientList(row);
        if (mergePatient?.length) {
          let len = mergePatient?.length;
          if (len >= viewNumber) {
            return mergePatient
          } else {
            let fillArr = Array(viewNumber - len).fill({});
            return [...mergePatient, ...fillArr]
          }
        } else {
          return Array(viewNumber).fill({});
        }
      }
    },

    //候诊患者信息
    getWaitPatientContent() {
      return function (row) {
        let orderNoInfo;
        let {orderTag, showRoomName, patientRoomNameKey, roomNamePosition = 'right'} = this.optionsInfo;
        if (orderTag && (row?.orderTagColor)) {
          orderNoInfo = (row.orderNo) ?
              `<span style='color:${row?.orderTagColor || 'transparent'}'>${row?.orderPrefix || '\u00a0'}</span>${row?.orderNumber || '\u00a0'}` : ''
        } else {
          orderNoInfo = `${row?.orderNo || '\u00a0'}`
        }

        // 1: 无诊室 2:诊室
        switch (showRoomName) {
          case 1:
            if (!!this.$lodash.trim(orderNoInfo)) {
              return `${orderNoInfo} ${row?.patientName || '\u00a0'}`
            } else {
              return `${row?.patientName || '\u00a0'}`
            }
          case 2:
            let patientInfo;
            if (!!this.$lodash.trim(orderNoInfo)) {
              patientInfo = `${orderNoInfo} ${row?.patientName || '\u00a0'}`
            } else {
              patientInfo = `${row?.patientName || '\u00a0'}`
            }
            if (roomNamePosition === 'right') {
              return `${patientInfo || '\u00a0'} ${row[patientRoomNameKey] || '\u00a0'}`
            } else {
              return `${row[patientRoomNameKey] || '\u00a0'} ${patientInfo || '\u00a0'}`
            }
        }
      }
    },
  },

  methods: {
    // 等待候诊滚动结束
    handleScrollEnd() {
      console.log('等待就诊滚动结束');
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "patientListInfo":
          let patientList = this.$lodash.cloneDeep(data);
          this.widgetKey = uuid();
          this.$store.dispatch('setWaitingPatientInfo', patientList)
          break
      }
    }
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-row-waiting-widget {
  background-color: transparent;
  color: #333;
  display: flex;
  flex-direction: row;
  align-self: center;
  align-items: center;
  margin: 20px 14px 10px 14px;
  overflow: hidden;
  box-sizing: border-box;

  .queue {
    font-size: 46px;
    flex: 1;
    height: 100%;
    padding: 0 8px;
    text-align: center;
    overflow: hidden;
    box-sizing: border-box;

    .queue-inside {
      border-radius: 18px;
      overflow: hidden;
      width: 100%;
      height: 100%;

      .dept {
        color: white;
        padding: 0 40px;
        font-size: 46px;
        height: 90px;
        background: rgba(0, 89, 179, 1);
        display: flex;
        flex-basis: 100%;
        text-align: center;
        align-self: center;
        align-items: center;
        justify-content: center;
      }

      .current-patient {
        background: rgba(51, 122, 194, 1);
        padding: 24px 0 22px 0;
        color: white;
        font-size: 45px;
        min-height: 86px;
        @include singleEllipsis(100%);
        display: block;
      }

      .wait-patient {
        display: flex;
        width: 100%;
        height: calc(100% - 90px);
        background: white;
        overflow: hidden;

        .scroll {
          width: 100%;
        }

        .inside-content {
          display: flex;
          flex-direction: column;
          width: 100%;
          align-items: center;
          align-self: center;
          align-content: center;
          justify-content: center; /* 如果你也想水平居中 */

          .wait-patient-inside {
            color: #333333;
            box-sizing: border-box;
            font-size: 32px;
            width: 100%;
            display: flex;
            height: calc(100% / 5);
            overflow: hidden;
            padding: 0 14px;
            background: rgba(255, 255, 255, 1);
            @include singleEllipsis(100%);
            text-align: center;
            border-bottom: 1px rgba(51, 51, 51, 0.2) solid;
          }
        }


        .wait-patient-inside-empty {
          min-height: 480px;
          margin-top: 10px;
          background: white;
        }
      }
    }
  }
}
</style>